package com.cloud.pole.param.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.api.param.ParamAreaApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.msg.ObjectRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.param.biz.ParamAreaBiz;
import com.cloud.pole.param.biz.PickListBiz;
import com.cloud.pole.param.model.entity.ParamArea;
import com.cloud.pole.param.model.entity.Picklist;
import com.cloud.pole.param.service.ParamAreaService;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("area/front")
@Slf4j
public class ParamAreaController extends BaseController<PickListBiz,Picklist> {
	
	@Autowired
	private ParamAreaService paramAreaService;
	@Autowired
	private ParamAreaBiz paramAreaBiz;

	
	@ResponseBody
	@RequestMapping("/getProvinceList")
    public ApiRestResponse<List<ParamAreaApi>> getProvinceList(){   	
    	try {
    		List<ParamArea> srcList = paramAreaService.getAreaList(ParamAreaApi.AREA_CODE_ROOT, ParamAreaApi.AREA_TYPE_PROVINCE);
			List<ParamAreaApi> descList = new ArrayList<ParamAreaApi>();
			if(srcList != null) {
				for(ParamArea paramArea : srcList) {
					ParamAreaApi paramAreaVo = new ParamAreaApi();
					BeanUtils.copyProperties(paramArea, paramAreaVo);
					descList.add(paramAreaVo);
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, descList);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getCityList")
    public ApiRestResponse<List<ParamAreaApi>> getCityList(
    		@RequestParam(defaultValue = "") String provinceId){   	
    	try {
			ParamException.err(StringUtils.isBlank(provinceId), "provinceId不能为空");
    		List<ParamArea> srcList = paramAreaService.getAreaList(provinceId, ParamAreaApi.AREA_TYPE_CITY);
			List<ParamAreaApi> descList = new ArrayList<ParamAreaApi>();
			if(srcList != null) {
				for(ParamArea paramArea : srcList) {
					ParamAreaApi paramAreaVo = new ParamAreaApi();
					BeanUtils.copyProperties(paramArea, paramAreaVo);
					descList.add(paramAreaVo);
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, descList);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getDistrictIdList")
    public ApiRestResponse<List<ParamAreaApi>> getDistrictIdList(
    		@RequestParam(defaultValue = "") String cityId){   	
    	try {
			ParamException.err(StringUtils.isBlank(cityId), "cityId不能为空");
    		List<ParamArea> srcList = paramAreaService.getAreaList(cityId, ParamAreaApi.AREA_TYPE_DISTRICT);
			List<ParamAreaApi> descList = new ArrayList<ParamAreaApi>();
			if(srcList != null) {
				for(ParamArea paramArea : srcList) {
					ParamAreaApi paramAreaVo = new ParamAreaApi();
					BeanUtils.copyProperties(paramArea, paramAreaVo);
					descList.add(paramAreaVo);
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, descList);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
